package com.rxy.rabbitmq.api.limit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
/**
 * 消费端限流
 * @author ruoxiyuan
 *
 */
public class Consumer {

	public static void main(String[] args) throws Exception {
		//1 创建ConnectionFactory
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.43.157");
		connectionFactory.setPort(5672);
		connectionFactory.setVirtualHost("/");
		//2 获取Connection
		Connection connection = connectionFactory.newConnection();
		//3 通过Connection创建一个新的Channel
		Channel channel = connection.createChannel();
		
		String exchangeName = "test_qos_exchange";
		String queueName = "test_qos_queue";
		String routingKey = "qos.#";
		//4 声明交换机和队列，然后进行绑定设置路由Key
		channel.exchangeDeclare(exchangeName, "topic", true, false, null);
		channel.queueDeclare(queueName, true, false, false, null);
		channel.queueBind(queueName, exchangeName, routingKey);
		
		//进行参数设置：单条消息的大小限制，一次最多能处理多少条消息，是否将上面设置应用于channel
		channel.basicQos(0, 1, false);
		
		//限流： autoAck设置为 false
		channel.basicConsume(queueName, false, new MyConsumer(channel));
	}
}
